# --------------------------
# main.py 优化后
# --------------------------
if __name__ == "__main__":
    from secure_config import SecureConfigLoader
    from logger import configure_logger
    from task_manager import TaskOrchestrator
    import logging
    import os

    # 初始化基础组件
    config = SecureConfigLoader.load("config.yaml") # 默认读取开发环境配置文件

    # 根据系统环境变量配置来选择读取配置文件,需要先添加环境变量
    node_env = os.getenv("NODE_ENV")
    if not node_env:
        raise ValueError("请设置环境变量 CONFIG_KEY")

    if node_env == "Development":
        config = SecureConfigLoader.load("config.yaml")
    elif node_env == "Production":
        config = SecureConfigLoader.load("deploy_config.yaml")

    print("Loaded browser config:", config['global']['browser'])  # 添加验证

    configure_logger(config['app']['log_path'])

    # 启动任务协调器
    orchestrator = TaskOrchestrator(config)
    try:
        orchestrator.start()
    except Exception as e:
        logging.critical(f"System crash: {str(e)}")
        orchestrator.shutdown()